package team.pikachu.pikachuclient.guide;

import android.support.v4.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import org.androidpn.client.ServiceManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import team.pikachu.pikachuclient.R;
import team.pikachu.pikachuclient.message.MsgItemFragment;
import team.pikachu.pikachuclient.message.MsgUploadActivity;
import team.pikachu.pikachuclient.teacher.PersonFragment;
import team.pikachu.pikachuclient.utils.Constants;
import team.pikachu.pikachuclient.utils.IPConstants;
import team.pikachu.pikachuclient.utils.LogUtil;

/**
 * Created by Alex on 2017/9/10.
 * <p>
 * 底部导航页面
 */
public class GuideActivity extends AppCompatActivity {
    private static final String LOGTAG = LogUtil.makeLogTag(GuideActivity.class).toString();

    private static final int WAITING = 2;
    private static final int FINISHED = 3;

    private Fragment msgItemFragment;
    private Fragment personFragment;

    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;

    private SharedPreferences pre;

    private int roleId;

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            if (roleId == Constants.ROLE_STUDENT) {
                return false;
            } else if (roleId == Constants.ROLE_TEACHER) {
                Fragment fragment = null;
                switch (item.getItemId()) {
                    case R.id.navigation_home:
                        fragment = msgItemFragment;
                        break;
                    case R.id.navigation_dashboard:
                        fragment = personFragment;
                        break;
                }
                fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fragment_container, fragment);
                fragmentTransaction.commit();
                return true;
            } else if (roleId == Constants.ROLE_WORKER) {
                int status = 0;
                switch (item.getItemId()) {
                    case R.id.navigation_waiting:
                        status = WAITING;
                        break;
                    case R.id.navigation_finished:
                        status = FINISHED;
                        break;
                }
                ((MsgItemFragment)msgItemFragment).onUpdate(status);
                return true;
            }
            return false;
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_upload) {
            Intent intent = new Intent(this, MsgUploadActivity.class);
            startActivity(intent);
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        ButterKnife.bind(this);


        pre = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        roleId = pre.getInt(getString(R.string.pre_roleid), 1);

        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        if (roleId == 2) {
            navigation.setVisibility(View.VISIBLE);
            navigation.inflateMenu(R.menu.navigation);
        }
        if (roleId == 3) {
            navigation.setVisibility(View.VISIBLE);
            navigation.inflateMenu(R.menu.worker);
        }

        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        toolbar.setOverflowIcon(getDrawable(R.drawable.ic_add_black_24dp));
        setSupportActionBar(toolbar);

        setDefaultFragment();
//        startService();
    }


    /**
     * 初始化fragment，并在view中添加fragment
     */
    private void setDefaultFragment() {
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        msgItemFragment = MsgItemFragment.newInstance();
        personFragment = PersonFragment.newInstance();
        if (roleId == 2) {
            fragmentTransaction.replace(R.id.fragment_container, personFragment);
            fragmentTransaction.commit();
        } else {
            fragmentTransaction.replace(R.id.fragment_container, msgItemFragment);
            fragmentTransaction.commit();
        }
    }

    /**
     * 启动推送服务
     */
    private void startService() {
        String mUsername = getIntent().getStringExtra("USERNAME");
        Log.d(LOGTAG, mUsername);
        ServiceManager serviceManager = new ServiceManager(this);
        serviceManager.setNotificationIcon(R.mipmap.notification);
        serviceManager.setAlias(mUsername);
        serviceManager.startService();
    }


    @Override
    public void onBackPressed() {
        moveTaskToBack(true);
    }
}
